﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using LLFM.CAT.Service;
using System.IO;
using LLFM.CAT.Model;

public partial class Admin_Product_AddProductImagList : AdminPage
{
    public int productId { get { return RequestHelper.GetInt("productId", 0); } }
    public int seq { get { return RequestHelper.GetInt("seq", 0); } }
    public string title = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        title = ProductsService.GetProductById(productId).Name;
    }

    protected void SubmitBtn(object sender, EventArgs e)
    {

        HttpPostedFile xiaoImg = xiaoFileUpload.PostedFile;
        HttpPostedFile zhongImg = zhongUpload.PostedFile;
        HttpPostedFile daImg = daFileUpload.PostedFile;
        string smallImageUrl = GetUrl(xiaoImg, title, seq + 1, "");
        string midImageUrl = GetUrl(zhongImg, title, seq + 1, "mid");
        string bigImageUrl = GetUrl(daImg, title, seq + 1, "big");
        ProductsImagesService.CreateImages(midImageUrl, smallImageUrl,  bigImageUrl, title,  productId, seq+1,(short)1);
        if(seq == 0)
        {
            M_Products products = ProductsService.GetProductById(productId);
            ProductsService.UpdateProduct(products.Id, products.SeriesId, products.Name,
            products.Model, products.BucketCapacity, products.WorkWeight, products.Engine,
            products.Power, products.ApplyTo, products.ShortDescription,
            products.Advantage, products.CoverImageId, midImageUrl
            , products.SeriesListImageBig
            , products.SeriesDetailImageBig
            , products.SeriesDetailImageList
            , products.UserManualImage, (short)1, products.UserManual);
        }
        Response.Redirect("ProductImagList.aspx?productId=" + productId);
    }

    private string GetUrl(HttpPostedFile jpeg_image_upload, string title, int seq, string imgtype)
    {
        string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };
        title = title + seq + imgtype;
        int imgWidth;
        int imgHeight;
        string subPath = string.Empty;
        string fullPath = Common.FileUploadHelper.BuildSavePath(jpeg_image_upload, System.Configuration.
            ConfigurationManager.AppSettings["ImagePath"], "ProductImg", out subPath, title);
        string mapPaht = Server.MapPath("~" + Path.GetDirectoryName(fullPath) + "/");

        Common.Enums.FileUploadResult result = Common.FileUploadHelper.FileUpload(jpeg_image_upload, mapPaht, Path.GetFileName(fullPath), 5242880, filetype, "", out imgWidth, out imgHeight);
        return ResolveUrl("~" + fullPath.ToLower().Replace("\\", "/").Replace("/websites", ""));
    }
}
